package ide.ccf.ccproject.editors;

import ide.ccf.ccproject.common.EditorInputWatcher;
import ide.ccf.ccproject.common.IEditor;
import ide.ccf.ccproject.pages.ResourceTreePage;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;

/**
 * 资源编辑器
 *
 */
public class ResourceEditor extends FormEditor implements IEditor 
{
	private ResourceTreePage treePage = null;
	private EditorInputWatcher listener;
	
	/**
	 * 构造器
	 */
	public ResourceEditor() 
	{
		super();
	}

	/**
	 * 初始化
	 */
	public void init(IEditorSite site, IEditorInput input) throws PartInitException 
	{
		super.init(site, input);
		listener = new EditorInputWatcher(this);
	}

	/**
	 * 重新载入
	 */
	public void reload() 
	{
		treePage.reload();
	}

	/**
	 * 保存
	 */
	public void doSave(IProgressMonitor monitor)
	{
		treePage.doSave(monitor);
		listener.updateTimeStamp();
	}
	
	/**
	 * 另存为
	 */
	public void doSaveAs()
	{
	
	}
	
	/**
	 * 是否允许另存为
	 */
	public boolean isSaveAsAllowed()
	{
		return false;
	}
	
	/**
	 * 添加页面
	 */
	protected void addPages()
	{
		setPartName(getEditorInput().getName());
		
		try 
		{
			treePage = new ResourceTreePage(this);
			addPage(treePage);
		} 
		catch(Exception e) 
		{
			
		}
	}
	
	/**
	 * 编辑器数据是否过时
	 */
	public boolean isDirty() 
	{
		return treePage.isDirty();
	}
	
	/**
	 * 页面变化
	 */
	protected void pageChange(int newPageIndex) 
	{
		super.pageChange(newPageIndex);
		
		if (newPageIndex == 0)
			treePage.setFocus();
	}

	/**
	 * 释放
	 */
	public void dispose() 
	{
		listener.dispose();
		super.dispose();
	}
	
	/**
	 * 设置焦点
	 */
	public void setFocus() 
	{
		super.setFocus();
		
		if (getCurrentPage() == 0)
			treePage.setFocus();
	}
}